package com.s7.app.delegate;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.lang.reflect.ParameterizedType;

/**
 * 带有标题栏的基础布局
 * 		可被自定义标题栏
 */
public abstract class BaseBarDelegate<T extends BaseViewInter> extends BaseADelegate {

    protected ViewGroup center;

    protected TextView tvBaseBarLeft, tvBaseBarTitle, tvBaseBarRight;

    @Override
    public void onCreate(LayoutInflater inflater, Bundle savedInstanceState) {

        ParameterizedType pt = (ParameterizedType) this.getClass()
                .getGenericSuperclass();
        Class<T> clazz = (Class<T>) pt.getActualTypeArguments()[0];
        BaseViewInter baseViewInter;
        try {
            baseViewInter = clazz.newInstance();
        } catch (InstantiationException e) {
            throw new RuntimeException("create BaseViewInter error");
        } catch (IllegalAccessException e) {
            throw new RuntimeException("create BaseViewInter error");
        }

        mContext = inflater.getContext();
        rootView = inflater.inflate(baseViewInter.getBaseLayoutId(), null, false);
        View centerView = inflater.inflate(getLayoutId(), null, false);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
                RelativeLayout.LayoutParams.FILL_PARENT);
        centerView.setLayoutParams(layoutParams);
        center = rootView.findViewById(baseViewInter.getBaseCenterLayoutId());
        center.addView(centerView);
        tvBaseBarTitle = rootView.findViewById(baseViewInter.getBaseBarTitleId());
        tvBaseBarLeft = rootView.findViewById(baseViewInter.getBaseBarLeftId());
        tvBaseBarRight = rootView.findViewById(baseViewInter.getBaseBarRightId());

        baseViewInter.initBaseView(rootView);
    }

    /** 初始化标题按按钮 */
    private void setBaseTitle(String title) {
        tvBaseBarTitle.setText(title);
    }

    /** 初始化右边按钮 */
    private void setBaseRight(Drawable drawable, String rightText) {
        tvBaseBarRight.setText(rightText);

        if (drawable != null) {
            tvBaseBarRight.setCompoundDrawablesWithIntrinsicBounds(drawable,
                    null, null, null);
        }
    }

    /**
     * 初始化标题栏
     *
     */
    public void initBaseTitleBar(int titleId) {
        initBaseTitleBar(getString(titleId));
    }

    /**
     * 初始化标题栏
     */
    public void initBaseTitleBar(String title) {
        setBaseTitle(title);
    }

    /**
     * 初始化标题栏
     */
    protected void initBaseTitleBar(String title, String rightText) {
        setBaseTitle(title);
        setBaseRight(null, rightText);
    }

    /**
     * 初始化标题栏
     */
    protected void initBaseTitleBar(String title, String rightText,
                                    int rightResId) {
        setBaseTitle(title);
        setBaseRight(getResources().getDrawable(rightResId), rightText);
    }
}
